﻿using System;
using System.Collections.Generic;
using System.Text;
using Nextensions.Text;

namespace Nextensions.Net.HTML
{
    public class Form: Element
    {
        private string _html = String.Empty;
        private Dictionary<string, Input> _inputElements = new Dictionary<string, Input> ();

        public Form(string HTML):base(HTML)
        {
            ElementFactory<Input>.CreateChildElements(this, Tags.Input, Tags.Select);
        }

        public override void AddElement(Element Element)
        {
            Input input = Element as Input;

            if (input == null)
                return;

            if (input.Type == InputTypes.Radio || input.Type == InputTypes.Checkbox)
            {
                if (!_inputElements.ContainsKey(input.Name))
                    _inputElements.Add(input.Name, input);
                else
                {
                    if (input.Selected)
                        _inputElements[input.Name] = input;
                }
            }
            else
            {
                if(!String.IsNullOrEmpty(input.Name) && !_inputElements.ContainsKey(input.Name))
                    _inputElements.Add(input.Name, input);
            }
        }

        public Input this[string elementName]
        {
            get { return ElementsDictionary[elementName]; }
        }

        public Dictionary<string, Input> ElementsDictionary
        {
            get
            {
                return new Dictionary<string,Input>(_inputElements);
            }
        }

        public List<Input> Elements
        {
            get
            {
                return new List<Input>(_inputElements.Values);
            }
        }

        public bool ContainsElement(string element)
        {
            return ElementsDictionary.ContainsKey(element);
        }

        public List<Input> GetInputElementsByType(InputTypes Type)
        {
            List<Input> list = new List<Input>();

            foreach (Input el in Elements)
            {
                if (el.Type == Type)
                    list.Add(el);
            }

            return list;
        }

        public string Action
        {
            get 
            {
                return MLUtil.GetAttributeValue(HTML, "action");
            }
        }

        public override string ToString()
        {
            return ToString(false);
        }

        public string ToString(bool encode)
        {
            StringBuilder text = new StringBuilder(Elements.Count * 32);

            for (int i = 0; i < Elements.Count; i++)
            {
                text.AppendFormat(
                    "{0}={1}&", 
                    encode ? Uri.EscapeDataString(Elements[i].Name) : Elements[i].Name, (encode ? Uri.EscapeDataString(Elements[i].Value) : Elements[i].Value).ToString().Replace("%20", "+"));
            }

            text.Remove(text.Length - 1, 1);

            return text.ToString();
        }

        public void AddOrUpdateElement(Input input)
        {
            if (ContainsElement(input.Name))
                this[input.Name].Value = input.Value;
            else
                AddElement(input);
        }
    }
}
